﻿using System;
using System.Reflection;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ZilLion.Service.Core.Infrastructure.Middleware.Error;
using ZilLion.Service.Core.Ioc;
using ZilLion.Service.Core.Snowflake;
using ZilLion.Service.Core.Starter;
using ZilLion.Service.Core.Starter.Config;
using ZilLion.Service.Core.Starter.Extention;
using ZilLion.Service.Core.WebSocket;

namespace ZilLion.Service.Core.App
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            SnowflakeIdCreater.Instance().Init(); //初始化全局雪花ID
        }

        public IConfiguration Configuration { get; }

        /// <summary>
        /// autofac固定config方法
        /// </summary>
        /// <param name="builder"></param>
        public void ConfigureContainer(ContainerBuilder builder)
        {
            IocConfig.Configure(builder, Assembly.GetExecutingAssembly());
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddZilLionServiceCore();
            services.AddResponseCaching();
            services.AddWebSocketManager();
        }

        /// <summary>
        ///  This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="serviceProvider"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
        {
           
            app.UseErrorHandling();
            app.UseCors(builder =>
                builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor |
                                   ForwardedHeaders.XForwardedProto
            });
            app.UseResponseCaching();
            app.UseSwagger();
          
            StaticConfig.Configure(app, env);
            app.UseSwaggerUI(options =>
            {
                options.InjectOnCompleteJavaScript("/swagger_lang.js");
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "MvcApp API V1");
                options.ShowJsonEditor();
            }); 
            var wsOptions = new WebSocketOptions
            {
                KeepAliveInterval = TimeSpan.FromSeconds(60),
                ReceiveBufferSize = 4 * 1024
            };
            app.UseWebSockets(wsOptions);
            //app.MapWebSocketManager(serviceProvider);
            app.UseMvc();
            app.MapWebSocketManager();
            app.UseSpecialRoute();
          
        }
    }
}